#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>
#include <openssl/ssl.h>
#include <openssl/opensslv.h>
#include <iostream>
void MessageCallback(AMQP::TcpChannel *channel, const AMQP::Message &message, uint64_t deliveryTag,bool redelivered)
{
    std::string msg ;
    msg.assign(message.body(), message.bodySize());
    std::cout << "Message received: " <<msg << std::endl;
    channel->ack(deliveryTag);//消息确认
}
int main()
{
    //实例化底层网络通信框架的IO事件监控句柄
    auto *loop = EV_DEFAULT;
    //实例化libEvHandler对象，将AMQP框架与事件监控关联起来
    AMQP::LibEvHandler handler(loop);
    //实例化连接对象
    AMQP::Address address("amqp://root:123456@127.0.0.1:5672/");
    AMQP::TcpConnection connection(&handler, address);
    //实例化信道对象
    AMQP::TcpChannel channel(&connection);
    //声明交换机
    channel.declareExchange("test-exchange", AMQP::ExchangeType::direct)
    .onError([](const char *message) {
        std::cout << "Error: " << message << std::endl;
        exit(1);
    })
    .onSuccess([]() {
        std::cout << "Exchange declared" << std::endl;
    });
    //声明队列
    channel.declareQueue("test-queue")
    .onError([](const char *message) {
        std::cout << "Error: " << message << std::endl;
        exit(1);
    })
    .onSuccess([]() {
        std::cout << "Queue declared" << std::endl;
    });

    //针对交换机和队列进行绑定
    channel.bindQueue("test-exchange", "test-queue", "test-queue")
    .onError([](const char *message) {
        std::cout << "Error: " << message << std::endl;
        exit(1);
    })
    .onSuccess([]() {
        std::cout << "Queue bound success" << std::endl;
    });
    //订阅队列消息--设置回调函数
    auto callback = std::bind(MessageCallback, &channel,std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
    channel.consume("test-queue", "comsume-tag")
    .onReceived(callback)
    .onError([](const char *message) {
        std::cout << "Error: " << message << std::endl;
        exit(1);
    });
    
    //启动底层网络通信接口
    ev_run(loop, 0);
    return 0;
}